<template>
  <div>
    <div>
      <h3>进制转换</h3>
      <div style="display: flex">
        <p>十进制:</p>
        <el-input
          v-model="input"
          style="width: 300px; margin: 20px"
          placeholder="请输入内容"
        ></el-input>
        <el-button style="height: 30px; margin: 20px" @click="trans"
          >转换</el-button
        >
        <el-select
          v-model="value"
          @change="trans"
          style="width: 100px; margin: 20px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input
          v-model="output"
          style="width: 300px; margin: 20px"
        ></el-input>
      </div>
    </div>
    <div>
      <h3>斐波那契数列</h3>
      <div style="display: flex"></div>
    </div>
  </div>
</template>

<script>
import Stack from "../../utils/stack";
import { defineComponent, ref, reactive, toRefs } from "vue";
export default {
  setup() {
    const state = reactive({
      input: "",
      output: "",

      options: [
        {
          value: 2,
          label: "二进制",
        },
        {
          value: 8,
          label: "八进制",
        },
        {
          value: 16,
          label: "十六进制",
        },
      ],
      value: 2,
    });

    const decimalToBinary = (decNumber, base) => {
      const stack = new Stack();
      let number = decNumber;
      let rem;
      let binaryString = "";
      while (number > 0) {
        rem = Math.floor(number % base);
        stack.push(rem);
        number = Math.floor(number / base);
      }
      while (!stack.isEmpty()) {
        //处理十六进制
        let hex = stack.pop();
        let char = ["A", "B", "C", "D", "E", "F"];
        if (hex > 9) {
          hex = char[hex - 10];
        }
        binaryString += hex.toString();
        // binaryString += stack.pop().toString();
      }
      return binaryString;
    };
    // function decimalToBinary(decNumber) {}

    const trans = () => {
      let count = state.input;
      let base = state.value;
      console.log(count, base);
      state.output = decimalToBinary(count, base);
      //   console.log(decimalToBinary(count));
    };
    // console.log(decimalToBinary(233)); // 11101001

    //阶乘
    function factorial(n) {
      if (n === 1 || n === 0) {
        return 1;
      }
      return n * factorial(n - 1);
    }
    console.log(factorial(5)); // 120

    // 斐波那契数列
    function fibonacci(n) {
      if (n < 1) {
        return 0;
      } else if (n === 1 || n === 2) {
        return 1;
      } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
      }
    }
    console.log(fibonacci(6)); // 8

    return { ...toRefs(state), trans, factorial, fibonacci };
  },
};
</script>

<style>
</style>